#!/usr/bin/env python
# _*_coding:utf-8_*_

"""
@Time :    2019/9/28 20:15
@Author:  ThinHeader
@File: redis_tool.py
@Software: PyCharm
"""
import pickle

import redis
from django.conf import settings


class RedisStat(object):
    """redis数据"""
    def __init__(self,
                 host=settings.SESSION_REDIS_HOST,
                 port=settings.SESSION_REDIS_PORT,
                 db=settings.SESSION_REDIS_DB,
                 password=settings.SESSION_REDIS_PASSWORD):
        self.r = redis.StrictRedis(host=host, port=port, db=db, password=password)

    def set_ttl(self, key, value, expire):
        self.r.set(key, value, expire)

    def set_obj_ttl(self, key, obj, expire):
        value = pickle.dumps(obj)
        self.r.set(key, value, expire)

    def set_obj(self, key, obj):
        value = pickle.dumps(obj)
        self.r.set(key, value)

    def set(self, key, value):
        self.r.set(key, value)

    def get_keys(self, pattern):
        return self.r.keys(pattern)

    def add_set_members(self, key, value):
        return self.r.sadd(key, value)

    def get_set_members(self, key):
        return self.r.smembers(key)

    def get(self, key):
        return self.r.get(key)

    def get_obj(self, key):
        obj_temp = self.r.get(key)
        if obj_temp:
            obj_info = pickle.loads(obj_temp)
        else:
            obj_info = None
        return obj_info

    def ttl(self, key):
        return self.r.ttl(key)

    def incr(self, key):
        return self.r.incr(key)

    def delete(self, key):
        return self.r.delete(key)
